<?php

/**
 * 过户模块
 */

namespace AuctionBack\Controller;

use AuctionBack\Logic\TransferLogic;
use AuctionCore\Account\Back\Employee;
use AuctionBack\Logic\OrderLogic;
use AuctionBack\Logic\CarReportLogic;
use AuctionCore\CarResource\CarModel;
use AuctionCore\Dispute\Dispute;
use AuctionCore\ChangeOrder\ChangeOrder;
use AuctionBack\Logic\ProceduresRequirementLogic;

class CarTransferController extends BaseController
{
    public function __construct()
    {
        parent::__construct();
        //页面头部信息
        $head = C('HEAD_INDEX');
        $this->assign("web_title", $head['title']);
        $this->assign("web_keywords", $head['keywords']);
        $this->assign("web_description", $head['description']);
        $this->emp_id = $this->user_info['emp_id'];
        //模板赋值
        $this->assign("emp_id", $this->emp_id);
    }

    public function index()
    {
        $this->display();
    }


    /**过户列表*****/
    public function getTransferList()
    {
        $page = I("get.p/d", 1);
        $size = 10;
        $status = I('get.status/d', 1);
        $orderType = I('get.order_type', 'p_order');
        $params = array();
        $params['status'] = $status;
        $params['emp_id'] = $this->user_info['emp_id'];
        $params['start_time'] = I('get.start_time', '');
        $params['end_time'] = I('get.end_time', '');
        $params['order_no'] = trim(I("get.order_no", ''));
        $params['contact_person'] = trim(I("get.contact_person", ''));
        $params['contact_mobile'] = trim(I("get.contact_mobile", ''));
        $params['car_plate_number'] = trim(I("get.car_plate_number", ''));
        $params['order_type'] = $orderType;
        $citys = $this->bc_city;
        $city_code = array();
        foreach ($citys as $key => $value) {
            $city_code[] = $key;
        }
        $bc_city = I("get.bc_city");
        if ($bc_city) {
            $params['bc_city'] = $bc_city;
        } else {
            $params['bc_city'] = $city_code;
        }
        //检查权限
        $rules = "GuoHuChaKanSuoYou";
        $e = new Employee($this->user_info['username']);
        $params['auth'] = $e->check_auth($rules);
        $transfer = D('Transfer', 'Logic');
        if ($orderType == 'd_order') {
            $result = $transfer->getOrderList($page, $size, $params);
        } else {
            $result = $transfer->getTransferList($page, $size, $params);
        }
        $Page = new \AuctionCore\Common\AjaxPage($result['total'], $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
        $show = $Page->show(); // 分页显示输出
        $show_from = \AuctionCore\Other\Channel::get_from();  //获取渠道来源信息
        $this->assign('show_from', $show_from);
        //模板赋值
        $this->assign('page', $show); // 赋值分页输出
        $this->assign('list', $result['list']);
        $this->assign("params", $params);
        $this->display("CarTransfer/list");
    }


    /**
     * 开始过户详情页
     */
    public function startTransfer()
    {
        $transfer_id = I("get.id/d");
        $transfer = D('Transfer', 'Logic');
        $transferInfo = $transfer->getTransferInfo($transfer_id);

        //获取手续上传要求
        $prLogic = new ProceduresRequirementLogic($transferInfo['sid']);
        $beforeTransfer = $prLogic->getListByTransferStatus(1);
        $afterTransfer = $prLogic->getListByTransferStatus(2);
        $this->assign('beforeTransfer', $beforeTransfer);
        $this->assign('afterTransfer', $afterTransfer);

        $show_from = \AuctionCore\Other\Channel::get_from();  //获取渠道来源信息
        $this->assign('show_from', $show_from);
        $this->assign('transferInfo', $transferInfo);
        $this->display();
    }

    /**
     * 确认开始过户
     */
    public function confirmTransfer()
    {
        $transfer_id = I('post.transfer_id/d');
        if ($transfer_id < 1) {
            $this->helpJsonResult(400, "参数错误");
        }
        $transfer = D('Transfer', 'Logic');
        $transferInfo = $transfer->getTransferLogInfo($transfer_id);
        if ($transferInfo['result'] > 0) {
            $this->helpJsonResult(400, "拍单过户状态已改变");
        }
        $o = new OrderLogic($transferInfo['order_id']);
        $orderInfo = $o->detail();
        if ($orderInfo['is_lock'] == 1) {
            $this->helpJsonResult(400, "拍单已锁定");
        }
        $transfer_limit = I('post.transfer_limit_id/d', 0);
        if ($transfer_limit == 0 || $transfer_limit == 3) {
            $this->helpJsonResult(400, '请选择车辆落户地');
        }
        $transfer_end_time = I('post.transfer_end_time');
        if (empty($transfer_end_time)) {
            $this->helpJsonResult(401, '过户截止时间');
        }
        $remark = I('post.reauction_remark');
        $data = [];
        $data['emp_id'] = $this->user_info['emp_id'];
        $data['transfer_id'] = $transfer_id;
        $data['transfer_to'] = $transfer_limit;
        $data['transfer_end_time'] = $transfer_end_time;
        $data['emp_id'] = $this->user_info['emp_id'];
        $data['remark'] = $remark;
        $ret = $transfer->confirmTransfer($data);
        if ($ret) {
            //记录员工操作日志
            $this->record_log($transferInfo['order_id'], $orderInfo['car_id'], '开始过户');
            $this->helpJsonResult(200, "确认成功");
        } else {
            $this->helpJsonResult(402, "确认失败或已确认,数据未更新。");
        }

    }

    /**
     * 过户中详情页
     */
    public function transfering()
    {
        $transfer_id = I("get.id/d");
        $transfer = D('Transfer', 'Logic');
        $transferInfo = $transfer->getTransferInfo($transfer_id);

        //获取手续上传要求
        $prLogic = new ProceduresRequirementLogic($transferInfo['sid']);
        $beforeTransfer = $prLogic->getListByTransferStatus(1);
        $afterTransfer = $prLogic->getListByTransferStatus(2);
        $this->assign('beforeTransfer', $beforeTransfer);
        $this->assign('afterTransfer', $afterTransfer);

        $show_from = \AuctionCore\Other\Channel::get_from();  //获取渠道来源信息

        //检查权限
        $rules = "CheLiangLuoHuXiuGai";
        $e = new Employee($this->user_info['username']);
        $auth = $e->check_auth($rules);
        $this->assign('auth', $auth);
        $this->assign('show_from', $show_from);
        $this->assign('transferInfo', $transferInfo);
        $this->display();
    }

    /**
     * 过户中继续跟踪
     */
    public function confirmTransferFollow()
    {
        $transfer_id = I('post.transfer_id/d');
        if ($transfer_id < 1) {
            $this->helpJsonResult(400, "参数错误");
        }
        $transfer = D('Transfer', 'Logic');
        $transferInfo = $transfer->getTransferLogInfo($transfer_id);
        if ($transferInfo['result'] != 1) {
            $this->helpJsonResult(400, "拍单过户状态已改变");
        }
        $o = new OrderLogic($transferInfo['order_id']);
        $orderInfo = $o->detail();
        if ($orderInfo['is_lock'] == 1) {
            $this->helpJsonResult(400, "拍单已锁定");
        }
        $transfer_to = I('post.transfer_to/d');
        if ($transfer_to == 0 || $transfer_to == 3) {
            $this->helpJsonResult(400, '请选择车辆落户地');
        }
        $deadlinetime = I('post.deadlinetime');
        if (empty($deadlinetime)) {
            $this->helpJsonResult(401, '请选择过户截止时间');
        }
        $track_remark = I('post.track_remark');
        $transfer_log = [];
        $transfer_log['transfer_id'] = $transfer_id;
        $transfer_log['transfer_to'] = $transfer_to;
        $transfer_log['deadlinetime'] = $deadlinetime;
        $transfer_log['track_remark'] = $track_remark;
        $transfer_log['emp_id'] = $this->user_info['emp_id'];
        $transferRes = $transfer->confirmTransferFollow($transfer_log);
        if ($transferRes) {
            //记录员工操作日志
            $this->record_log($transferInfo['order_id'], $orderInfo['car_id'], '过户跟踪');
            $this->helpJsonResult(200, "确认成功");
        } else {
            $this->helpJsonResult(402, "确认失败或已确认,数据未更新。");
        }
    }

    /**
     * 待提档继续跟踪
     */
    public function takeTransferFileFollow()
    {
        $transfer_id = I('post.transfer_id/d');
        if ($transfer_id < 1) {
            $this->helpJsonResult(400, "参数错误");
        }
        $transfer = D('Transfer', 'Logic');
        $transferInfo = $transfer->getTransferLogInfo($transfer_id);
        if ($transferInfo['result'] != 4) {
            $this->helpJsonResult(400, "拍单过户状态已改变");
        }
        $o = new OrderLogic($transferInfo['order_id']);
        $orderInfo = $o->detail();
        if ($orderInfo['is_lock'] == 1) {
            $this->helpJsonResult(400, "拍单已锁定");
        }
        $mentiontime = I('post.mentiontime');
        if (empty($mentiontime)) {
            $this->helpJsonResult(401, '请选择预计提档日期');
        }
        $track_remark = I('post.track_remark');
        $transfer_log = [];
        $transfer_log['transfer_id'] = $transfer_id;
        $transfer_log['mentiontime'] = $mentiontime;
        $transfer_log['track_remark'] = $track_remark;
        $transfer_log['emp_id'] = $this->user_info['emp_id'];
        $transferRes = $transfer->takeTransferFileFollow($transfer_log);
        if ($transferRes) {
            //记录员工操作日志
            $this->record_log($transferInfo['order_id'], $orderInfo['car_id'], '过户跟踪');
            $this->helpJsonResult(200, "确认成功");
        } else {
            $this->helpJsonResult(402, "确认失败或已确认,数据未更新。");
        }
    }

    /**
     * 外迁送档
     */
    public function transferSendFile()
    {
        $transfer_id = I('post.transfer_id/d');
        if ($transfer_id < 1) {
            $this->helpJsonResult(400, "参数错误");
        }
        $transfer = D('Transfer', 'Logic');
        $transferInfo = $transfer->getTransferLogInfo($transfer_id);
        if ($transferInfo['result'] != 1) {
            $this->helpJsonResult(400, "拍单过户状态已改变");
        }
        $o = new OrderLogic($transferInfo['order_id']);
        $orderInfo = $o->detail();
        if ($orderInfo['is_lock'] == 1) {
            $this->helpJsonResult(400, "拍单已锁定");
        }
        $mentiontime = I('post.mentiontime');
        if (!$mentiontime) {
            $this->helpJsonResult(401, '请选择预计提档日期');
        }
        $mention_remark = I("post.mention_remark");
        $data = [];
        $data['transfer_id'] = $transfer_id;
        $data['emp_id'] = $this->user_info['emp_id'];
        $data['mentiontime'] = $mentiontime;
        $data['mention_remark'] = $mention_remark;
        $is_take_car = I('post.is_take_car');
        if ($is_take_car == 1) {
            $man_id = I('post.man_id');
            if ($man_id < 1) {
                $this->helpJsonResult(401, '请选择提车人');
            }
            $data['man_id'] = $man_id;
        }
        $transferRes = $transfer->transferSendFile($data);
        if ($transferRes) {
            //记录员工操作日志
            $this->record_log($transferInfo['order_id'], $orderInfo['car_id'], '外迁送档');
            $this->helpJsonResult(200, "确认成功");
        } else {
            $this->helpJsonResult(402, "确认失败或已确认,数据未更新。");
        }
    }

    /**
     * 提档
     */
    public function takeTransferFileView()
    {
        $transfer_id = I("get.id/d");
        $transfer = D('Transfer', 'Logic');
        $transferInfo = $transfer->getTransferInfo($transfer_id);

        //获取手续上传要求
        $prLogic = new ProceduresRequirementLogic($transferInfo['sid']);
        $beforeTransfer = $prLogic->getListByTransferStatus(1);
        $afterTransfer = $prLogic->getListByTransferStatus(2);
        $this->assign('beforeTransfer', $beforeTransfer);
        $this->assign('afterTransfer', $afterTransfer);

        $show_from = \AuctionCore\Other\Channel::get_from();  //获取渠道来源信息
        $this->assign('show_from', $show_from);
        $this->assign('transferInfo', $transferInfo);
        $this->display();
    }

    /**
     * 过户成功
     */
    public function finishTransfer()
    {
        $transfer_id = I('post.transfer_id/d');
        if ($transfer_id < 1) {
            $this->helpJsonResult(400, "参数错误");
        }
        $transfer = D('Transfer', 'Logic');
        $transferInfo = $transfer->getTransferLogInfo($transfer_id);
        if ($transferInfo['result'] != 1 && $transferInfo['result'] != 4) {
            $this->helpJsonResult(400, "拍单过户状态已改变");
        }
        $o = new OrderLogic($transferInfo['order_id']);
        $orderInfo = $o->detail();
        if ($orderInfo['is_lock'] == 1) {
            $this->helpJsonResult(400, "拍单已锁定");
        }
        $is_show_take_man = I('post.is_show_take_man');
        $succ_remark = I("post.succ_remark");
        $data = [];
        $data['transfer_id'] = $transfer_id;
        $data['succ_remark'] = $succ_remark;
        $data['is_show_take_man'] = $is_show_take_man;
        $data['emp_id'] = $this->user_info['emp_id'];
        $data['emp_name'] = $this->user_info['real_name'];
        if ($is_show_take_man) {
            $man_id = I("post.man_id");
            if (empty($man_id)) {
                $this->helpJsonResult(401, "请选择提车人");
            }
            $data['man_id'] = $man_id;
        }
        $transferRes = $transfer->finishTransfer($data);
        if ($transferRes) {
            //记录员工操作日志
            $this->record_log($transferInfo['order_id'], $orderInfo['car_id'], '过户完成');
            $this->helpJsonResult(200, "确认成功");
        } else {
            $this->helpJsonResult(402, "确认失败或已确认,数据未更新。");
        }
    }

    /**
     * 过户成功 失败详情页
     */
    public function viewTransfer()
    {
        $transfer_id = I("get.id/d");
        $transfer = D('Transfer', 'Logic');
        $transferInfo = $transfer->getTransferInfo($transfer_id);

        //获取手续上传要求
        $prLogic = new ProceduresRequirementLogic($transferInfo['sid']);
        $beforeTransfer = $prLogic->getListByTransferStatus(1);
        $afterTransfer = $prLogic->getListByTransferStatus(2);
        $this->assign('beforeTransfer', $beforeTransfer);
        $this->assign('afterTransfer', $afterTransfer);

        $show_from = \AuctionCore\Other\Channel::get_from();  //获取渠道来源信息
        $alltype = C('TRANSFER_PROCEDURE_TYPE');
        $host = get_content_cdnurl();
        $this->assign('host', $host);
        $this->assign('alltype', $alltype);
        $this->assign('show_from', $show_from);
        $this->assign('transferInfo', $transferInfo);
        $this->display();
    }

    private function record_log($order_id, $car_id, $action)
    {
        //记录员工操作日志
        $log = [
            'emp_id' => $this->emp_id,
            'order_id' => $order_id,
            'car_id' => $car_id,
            'action' => $action
        ];
        \AuctionCore\System\SystemLog::add($log);

        /**
         * 记录车辆跟踪日志
         */
        $car_action_data = array(
            'action_name' => $action,
            'emp_id' => $this->emp_id
        );
        \AuctionCore\CarResource\Car::add_car_trace_log($car_id, $car_action_data);

        /**
         * 记录拍品跟踪日志
         */
        $order_action_data = array(
            'action_name' => $action,
            'emp_id' => $this->emp_id
        );
        \AuctionCore\AuctionOrder\AuOrder::add_order_trace_log($order_id, $order_action_data);
    }


    //违约
    public function violate()
    {
        $transfer_id = I("post.transfer_id/d", 0);
        if ($transfer_id < 1) {
            $this->helpJsonResult(400, "参数错误");
        }
        $transfer = D('Transfer', 'Logic');
        $transferInfo = $transfer->getTransferLogInfo($transfer_id);
        $o = new OrderLogic($transferInfo['order_id']);
        $orderInfo = $o->detail();
        if ($orderInfo['is_lock'] == 1) {
            $this->helpJsonResult(400, "拍单已锁定");
        }
        $poInfo = $o->getPurchaseByOrderId();
        $c = new CarReportLogic($orderInfo['car_id']);
        $carInfo = $c->detail();
        $car_plate_number = C('PLATE_PREFIX')[$carInfo['plate_prefix']] . $carInfo['plate_suffix'];
        $data = [];
        $data['order_id'] = $transferInfo['order_id'];
        $data['order_no'] = $orderInfo['order_no'];
        $data['po_id'] = $poInfo['id'];
        $data['po_no'] = $poInfo['purchase_order_nu'];
        $data['sid'] = $poInfo['sid'];
        $data['car_plate_number'] = $car_plate_number;
        $data['order_status'] = $orderInfo['status'];
        $data['source'] = 6;
        $data['originator'] = $this->emp_id;
        $res = ChangeOrder::CreateChangeOrder($data);
        if (!$res) {
            $this->record_log($transferInfo['order_id'], $orderInfo['car_id'], "过户发起违约");
            $this->helpJsonResult(400, '处理失败');
        } else {
            $this->helpJsonResult(200, '处理成功');
        }
    }

    /**争议页面***/
    public function dispute()
    {
        $transfer_id = I("get.transfer_id/d", 0);
        if ($transfer_id < 1) {
            $this->helpJsonResult(400, "参数错误");
        }
        $transfer = D('Transfer', 'Logic');
        $transferInfo = $transfer->getTransferLogInfo($transfer_id);
        $o = new OrderLogic($transferInfo['order_id']);
        $orderInfo = $o->detail();
        $c = new CarReportLogic($orderInfo['car_id']);
        $carInfo = $c->detail();
        $cm = new CarModel();
        $brand = $cm->get_model_by_id($carInfo['brand_id'], 'brand');
        $series = $cm->get_model_by_id($carInfo['series_id'], 'series');
        $model = $cm->get_model_by_id($carInfo['model_id']);
        $car_model = $brand['name'] . ' ' . $series['name'] . ' ' . $model['name'];
        $this->assign('transfer_id', $transfer_id);
        $this->assign('order_no', $orderInfo['order_no']);
        $this->assign('car_model', $car_model);
        $this->display();
    }

    //争议
    public function saveDispute()
    {
        $transfer_id = I("post.transfer_id/d", 0);
        if ($transfer_id < 1) {
            $this->helpJsonResult(400, "参数错误");
        }
        $transfer = D('Transfer', 'Logic');
        $transferInfo = $transfer->getTransferLogInfo($transfer_id);
        $remark = I("post.remark");
        $dispute_reason = I("post.dispute_reason");
        $o = new OrderLogic($transferInfo['order_id']);
        $orderInfo = $o->detail();
        if ($orderInfo['is_lock'] == 1) {
            $this->helpJsonResult(400, "拍单已锁定");
        }
        $poInfo = $o->getPurchaseByOrderId();
        $c = new CarReportLogic($orderInfo['car_id']);
        $carInfo = $c->detail();
        $car_plate_number = C('PLATE_PREFIX')[$carInfo['plate_prefix']] . $carInfo['plate_suffix'];
        $data = [];
        $data['order_id'] = $transferInfo['order_id'];
        $data['order_no'] = $orderInfo['order_no'];
        $data['po_id'] = $poInfo['id'];
        $data['po_no'] = $poInfo['purchase_order_nu'];
        $data['car_plate_number'] = $car_plate_number;
        $data['order_status'] = $orderInfo['status'];
        $data['od_id'] = $orderInfo['success_dealer_id'];
        $data['od_type'] = 1;
        $data['remark'] = $remark;
        $data['dispute_reason'] = $dispute_reason;
        $data['emp_id'] = $this->emp_id;
        $res = Dispute::DisputeAdd($data);
        if (!$res) {
            $this->helpJsonResult(400, '处理失败');
        } else {
            $this->record_log($transferInfo['order_id'], $orderInfo['car_id'], "过户发起争议");
            $this->helpJsonResult(200, '处理成功');
        }
    }


}